<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="分销商">
          <a-select v-decorator="['distributor_id', {rules: [{ required: true, message: '请选择分销商!' }]}]">
            <a-select-option value="">请选择</a-select-option>
            <a-select-option v-for="(item, index) in dictOption" :key="index" :value="item.id">
              <span style="display: inline-block;width: 100%">
                {{ item.name }}
              </span>
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="展会主题">
          <a-select v-decorator="['meeting_info_id', {rules: [{ required: true, message: '请选择展会主题!' }]}]" @change="numSel">
            <a-select-option value="">请选择</a-select-option>
            <a-select-option v-for="(item, index) in dictOptiond" :key="index" :value="item.id">
              <span style="display: inline-block;width: 100%">
                {{ item.theme }}
              </span>
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="主票数">
          <a-input-number :min="1" :max="1000" placeholder="请输入主票数" v-decorator="['meetTickets_mainNum', {rules: [{ required: true, message: '请输入主票数!' }]} ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="次票数">
          <a-input-number :min="1" :max="1000" placeholder="请输入次票数" v-decorator="['meetTickets_attachNum', {rules: [{ required: true, message: '请输入次票数!' }]} ]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import { getAction } from '@/api/manage'

  export default {
    name: "Exhibition_ticketsModal",
    data () {
      return {
        title:"操作",
        valselIndex:0,
        dictOption:[],
        dictOptiond:[],
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        url: {
          add: "/exhibition/exhibition_meeting_tickets/addDistributor2Tickets",
          edit: "/exhibition/exhibition_tickets/edit",
        },
      }
    },
    mounted (){
      
      var paramd={
        column:"createTime",
        order:"desc",
        field:"id,,,name,phone,remark,action",
        pageNo:1,
        pageSize: 1000
      };
      getAction("/exhibition/exhibition_distributor_info/list",paramd).then((res) => {
        if (res.success) {
          this.dictOption = res.result.records;
        }
        if(res.code===510){
          this.$message.warning(res.message)
        }
      })
      var params={
        column:"createTime",
        order:"desc",
        field:"id,,,name,phone,remark,action",
        pageNo:1,
        pageSize: 1000
      };
      getAction("/exhibition/exhibition_meeting_info/list",params).then((res) => {
        if (res.success) {
          this.dictOptiond = res.result.records;
        }
        if(res.code===510){
          this.$message.warning(res.message)
        }
      })
    },
    created () {
    },
    methods: {
      numSel(value,option){
        this.valselIndex = this.dictOptiond[option.key].ticketsNum;
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'worksNumber','address',"tickets"))
		  //时间格式化
          this.form.setFieldsValue({bindTime1:this.model.bindTime1?moment(this.model.bindTime1):null})
          this.form.setFieldsValue({bindTime2:this.model.bindTime2?moment(this.model.bindTime2):null})
          this.form.setFieldsValue({bindTime3:this.model.bindTime3?moment(this.model.bindTime3):null})
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            if(values.ticketsNum > this.valselIndex){
              that.$message.success("场次票数不足");
              return
            }
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            httpurl+=this.url.add;
            method = 'post';
            let formData = Object.assign(this.model, values);
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>